﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.QLNH_BUS_WS;

namespace QLNH.MyForms
{
    public partial class AdminAccountEdit : FormUC
    {
        #region Properties

        private bool _edit;
        private string _accountID;
        private string _accountType;

        #endregion

        #region Constructor

        public AdminAccountEdit(LayoutUC mainLayout)
        {
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_ACCOUNT_EDIT;

            InitializeComponent();

            _edit = false;
        }

        public AdminAccountEdit(LayoutUC mainLayout, params object[] values)
        {
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_ACCOUNT_EDIT;

            InitializeComponent();

            _edit = true;
            _accountID = values[0].ToString();
            this.tb_username.Text = values[1].ToString();
            this.tb_fullname.Text = values[2].ToString();
            _accountType = values[3].ToString();
        }

        #endregion

        #region Functions

        //kiem tra cac du lieu dien vao
        private void CheckAndSave(bool goBack)
        {
            if (this.tb_username.Text.Equals(""))
            {
                this.lb_warning.Text = "* Tên tài khoản không hợp lệ";
                this.lb_warning.Visible = true;
                return;
            }

            if (this.tb_fullname.Text.Equals(""))
            {
                this.lb_warning.Text = "* Họ tên không hợp lệ";
                this.lb_warning.Visible = true;
                return;
            }

            AccountDTO acc = new AccountDTO();
            acc.ID = _accountID;
            acc.UserName = this.tb_username.Text.Trim();
            acc.FullName = this.tb_fullname.Text.Trim();
            acc.RoleID = this.cb_accType.SelectedValue.ToString();

            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();

            if (_edit)
            {
                if (ws.EditAccount(acc) == true)
                {
                    MessageBox.Show("Cập nhật tài khoản thành công");
                    if (goBack)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_ACCOUNT_MANAGEMENT);
                    else
                    {
                        this.bt_save.Enabled = false;
                        this.bt_saveAndBack.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("Cập nhật tài khoản thất bại");
                }
            }
            else
            {
                if (ws.AddCount(acc) == true)
                {
                    MessageBox.Show("Thêm tài khoản thành công");
                    if (goBack)
                        this._mainLayout.SwitchForm(FormKey.ADMIN_ACCOUNT_MANAGEMENT);
                    else
                        ResetControlText();
                }
                else
                {
                    MessageBox.Show("Thêm tài khoản thất bại");
                }
            }
        }

        //xoa du lieu o cac control
        private void ResetControlText()
        {
            this.tb_username.Text = "";
            this.tb_fullname.Text = "";
            this.tb_username.Focus();
        }

        #endregion

        #region Control Events

        private void AdminAccountEdit_Load(object sender, EventArgs e)
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            RoleDTO[] roles = ws.GetAllRole();
            this.cb_accType.DataSource = roles;
            this.cb_accType.DisplayMember = "Name";
            this.cb_accType.ValueMember = "ID";

            if (_edit)
            {
                this.cb_accType.Text = _accountType;
                if (_accountType.Equals("admin"))
                    this.cb_accType.Enabled = false;
                this.bt_save.Enabled = false;
                this.bt_saveAndBack.Enabled = false;
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_ACCOUNT_MANAGEMENT);
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            CheckAndSave(false);
        }

        private void bt_saveAndBack_Click(object sender, EventArgs e)
        {
            CheckAndSave(true);
        }

        private void InfoChanged(object sender, EventArgs e)
        {
            if (_edit == true && this.bt_save.Enabled == false && this.bt_saveAndBack.Enabled == false)
            {
                this.bt_save.Enabled = true;
                this.bt_saveAndBack.Enabled = true;
            }

            if (this.lb_warning.Visible == true)
                this.lb_warning.Visible = false;
        }

        #endregion
    }
}
